﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLNS_Client
{
    public partial class frm_ThemDVHC : Form
    {
        string temp = "";
        string flag = "";
        ServiceReference1.Service_QLNSClient sv = new ServiceReference1.Service_QLNSClient();
        public frm_ThemDVHC()
        {
            InitializeComponent();
        }
        private void frm_ThemDVHC_Load(object sender, EventArgs e)
        {
            Load_DVHC();
            
        }
        public void Load_DVHC()
        {
            drg_DVHC.DataSource = sv.getDVHC();
        }

        private void drg_DVHC_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            txt_MaDVHC.Text = drg_DVHC.CurrentRow.Cells[0].Value.ToString();
            txt_TenDVHC.Text = drg_DVHC.CurrentRow.Cells[1].Value.ToString();
            temp = drg_DVHC.CurrentRow.Cells[0].Value.ToString();
            txt_MaDVHC.Enabled = false;
            txt_TenDVHC.Enabled = false;
            btn_LuuDVHC.Enabled = false;
            btn_HuyDVHC.Enabled = false;
        }
        private void btn_ThemDVHC_Click(object sender, EventArgs e)
        {
            flag = "them"; 
            btn_ThemDVHC.Enabled = true;
            btn_LuuDVHC.Enabled = true;
            btn_HuyDVHC.Enabled = true;
            btn_SuaDVHC.Enabled = false;
            btn_XoaDVHC.Enabled = false;
            btn_ThoatDVHC.Enabled = true;
            txt_MaDVHC.Enabled = true;
            txt_TenDVHC.Enabled = true;
            txt_MaDVHC.Text = "";
            txt_TenDVHC.Text = "";
            txt_MaDVHC.Focus();
        }

        private void btn_LuuDVHC_Click(object sender, EventArgs e)
        {
            string a = txt_MaDVHC.Text;
            string b = txt_TenDVHC.Text;
            ServiceReference1.DONVI_HANHCHINH dvhc = new ServiceReference1.DONVI_HANHCHINH();
            dvhc.Ma_DV_HC = a;
            dvhc.TenDVHC = b;

            if (a == "" | b == "")
            {
                MessageBox.Show("Nhập đầy đủ dữ liệu!");
            }
            else
            {
                int kq = 0;
                if (flag == "them")
                {
                    if (sv.check_dvhc(dvhc))
                        kq = -1;
                    else
                        kq = sv.addDVHC(dvhc);
                }
                else
                {
                    if (flag == "sua")
                    {
                        sv.setDVHC(dvhc);
                        kq = 1;
                    }
                }
                if (kq != -1)
                {
                    MessageBox.Show("Hoàn tất!");
                    Load_DVHC();
                    btn_SuaDVHC.Enabled = true;
                    btn_XoaDVHC.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Lôi!!!");
                }
            }
        }

        private void btn_HuyDVHC_Click(object sender, EventArgs e)
        {
            btn_HuyDVHC.Enabled = false;
            btn_LuuDVHC.Enabled = false;
            btn_SuaDVHC.Enabled = true;
            btn_XoaDVHC.Enabled = true;
        }

        private void btn_XoaDVHC_Click(object sender, EventArgs e)
        {
            ServiceReference1.DONVI_HANHCHINH dvhc = new ServiceReference1.DONVI_HANHCHINH();
            dvhc.Ma_DV_HC = temp;
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn xóa dòng này?", "warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if(dr == DialogResult.OK)
            {
                sv.deleteDVHC(dvhc);
                Load_DVHC();
                txt_MaDVHC.Text = "";
                txt_TenDVHC.Text = "";
            }
        }

        private void btn_SuaDVHC_Click(object sender, EventArgs e)
        {
            flag = "sua";
            btn_HuyDVHC.Enabled = true;
            btn_LuuDVHC.Enabled = true;
            txt_TenDVHC.Enabled = true;
        }

        private void btn_ThoatDVHC_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn thoát khỏi bảng này?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                this.Close();
            }
        }

    }
}
